package com.wipz.eoa.iphone.costmanagement;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wipz.eoa.iphone.BaseAction;
import com.wipz.eoa.iphone.businessProcess.AgencyCoreService;
import com.wipz.eoa.iphone.businessProcess.TaskTransitionService;
import com.wipz.eoa.iphone.util.Constants;

/**
 * 日常费用报销控制类
 * @author 张涛
 * @date 2012-2-10
 */

public class RegularExpensesAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	private IRegularExpensesService regularExpensesService;
	private Map view;
	private List list;
	private AgencyCoreService agencyCoreService;
	private TaskTransitionService taskTransitionService;
	private Map bxSubmit;

	
	public Map getBxSubmit() {
		return bxSubmit;
	}


	public void setBxSubmit(Map bxSubmit) {
		this.bxSubmit = bxSubmit;
	}


	public void setTaskTransitionService(TaskTransitionService taskTransitionService) {
		this.taskTransitionService = taskTransitionService;
	}


	public void setAgencyCoreService(AgencyCoreService agencyCoreService) {
		this.agencyCoreService = agencyCoreService;
	}


	public Map getView() {
		return view;
	}


	public void setView(Map view) {
		this.view = view;
	}


	public List getList() {
		return list;
	}


	public void setList(List list) {
		this.list = list;
	}


	public void setRegularExpensesService(
			IRegularExpensesService regularExpensesService) {
		this.regularExpensesService = regularExpensesService;
	}


	/**
	 * 查询所有
	 * author：张涛
	 * date：2012-2-10
	 * time：下午03:04:15
	 * return：String
	 */
	@SuppressWarnings("unchecked")
	public String list(){
		String commiterCode = this.getRequest().getParameter("COMMITERCODE");
		if(!"".equals(commiterCode) && commiterCode != null){
			Map params = new HashMap();
			params.put("COMMITERCODE", commiterCode);
			params.put("sort", this.getRequest().getParameter("sort"));
			params.put("order", this.getRequest().getParameter("order"));
			list = regularExpensesService.queryAll(params);
		}
		return SUCCESS;
	}
	
	/**
	 * 查看详细信息
	 * author：张涛
	 * date：2012-2-10
	 * time：下午05:08:10
	 * return：String
	 */
	@SuppressWarnings("unchecked")
	public String view(){
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		String taskId = this.getRequest().getParameter("taskId");
		String id =  this.getRequest().getParameter("id");
		Map params = new HashMap();
		params.put("ID",id);
		view = regularExpensesService.view(params);
		
		Map newMap = (Map) regularExpensesService.queryOne("businessProcess.queryByCode",view);
		String processId = newMap == null ? "":(String)newMap.get("PROCESSID") ;
		view.put("processInstanceId", processId);
		if(null == COMMITERCODE || COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		String userId = (String) regularExpensesService.queryOne("user.queryId",COMMITERCODE);
		view = agencyCoreService.agencyList(Constants.DAILYREIMBURSEMENT_BUSINESS_ID, userId,view);
		
		
		if(null == taskId || taskId.isEmpty()) {
			return SUCCESS;
		}
		
		view.put("taskId", taskId);
		List list =(taskTransitionService.getTaskTransition(taskId));
		Map url = new HashMap();
		StringBuffer bf = new StringBuffer();
		bf.append("/costmanagement/regularExpenses_bxSubmit.action?id=");
		bf.append(id);
		bf.append("&COMMITERCODE=");
		bf.append(COMMITERCODE);
		bf.append("&taskId=");
		bf.append(taskId);
		url.put("url", bf.toString());
		list.add(url);
		view.put("button", list);
		return SUCCESS;
	}
	
	/**
	 * 日常办公费用报销审批
	 * @return
	 * 2012-2-21 下午04:02:31
	 */
	public String bxSubmit() {
		//获取ID
		String id = this.getRequest().getParameter("id");
		//用户代号
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if(id.isEmpty() || COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		//任务唯一标识taskId
		String taskId =  this.getRequest().getParameter("taskId");
		//处理意见
		String commentContent =  this.getRequest().getParameter("comment");
		try {
			String s = new String(commentContent.getBytes("iso-8859-1"),"utf-8");
			commentContent = java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}

		//流向
		String transition = this.getRequest().getParameter("name");
		//处理类型
		String typeaccept = this.getRequest().getParameter("pass");
		
		Map param = new HashMap();
		param.put("COMMITERCODE", COMMITERCODE);
		param.put("id", id);
		param.put("taskId", taskId);
		param.put("comment", commentContent);
		param.put("transition", transition);
		param.put("pass", typeaccept);
		Map mapUuid = (Map) regularExpensesService.queryOne("user.queryAll", COMMITERCODE);
		param.put("uuitId",mapUuid.get("ORGID"));
		bxSubmit = regularExpensesService.bxSubmit(param);
		return SUCCESS;
	}
}
